
package com.sxkj.service.sys;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sxkj.entity.entity.sys.Dict;
import com.sxkj.entity.mapper.sys.DictMapper;
import com.sxkj.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author daixiaowei
 *
 * @Description: 字典
 * @date 2019-12-23 15:56:34
 */
@Service
public class DictService extends BaseService<DictMapper, Dict> {

	@Autowired
	private DictMapper dictMapper;

	/**
	* 获取单条数据
	* @param id
	* @return Dict
	*/
	public Dict get(String id) throws Exception{
		return super.get(id);
	}
	
	/**
	* 获取单条数据
	* @param dict
	* @return Dict
	*/
	public Dict get(Dict dict) throws Exception{
		return super.get(dict);
	}
	
	/**
	* 查询列表数据
	* @param dict
	* @return List<Dict>
	*/
	public List<Dict> findList(Dict dict) throws Exception{
		return super.findList(dict);
	}

	/**
	* 查询分页数据
	* @param dict
	* @param pageSize
	* @param pageNum
	* @return Page
	*/
	public PageInfo findPage(Dict dict, Integer pageSize, Integer pageNum) throws Exception{
		return super.findPage(dict, pageSize, pageNum);
	}

	/**
	* 保存数据（插入或更新）
	* @param dict
	*/
	@Transactional(rollbackFor = Exception.class)
	public void save(Dict dict) throws Exception{
		super.save(dict);
	}

	/**
	* 删除数据
	* @param dict
	*/
	@Transactional(rollbackFor = Exception.class)
	public void delete(Dict dict) throws Exception{
		super.delete(dict);
	}

	/**
	 *
	 * @title: searchListPage
	 * @author: daixiaowei
	 * @date: 2019年12月23日 下午 06:06:16
	 * @description:查询字典项列表
	 * @param: dicType pageSize pageNum
	 * @return:
	 */
	public List<Map<String,Object>> searchListPage(String dicType, Integer pageSize, Integer pageNum) {
		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		PageHelper.startPage(pageNum, pageSize);
		List<Dict> lists = dictMapper.selectList(dicType);
		if (null != list) {
			for(Dict d : lists){
				if(d.getType().equals(d.getDicType())){
					Map<String, Object> map = new HashMap<String,Object>();
					map.put("id", d.getId());
					map.put("code", d.getCode());
					map.put("name", d.getName());
					map.put("parentId", d.getParentId());
					map.put("parentIds", d.getParentIds());
					map.put("path", d.getPath());
					map.put("location", d.getLocation());
					map.put("type", d.getType());
					map.put("description", d.getDescription());
					map.put("hasChildren", d.isHasChildren() == true? "是":"否");
					if(d.isHasChildren()==true){
						map.put("children",dictChildSearch(d.getId()));
					}
					list.add(map);
				}
			}
		}
		return list;
	}
	private List<Dict> dictChildSearch(String id) {
		return dictMapper.selectDictChildSearch(id);
	}

	/**
	 *
	 * @title: add
	 * @author: daixiaowei
	 * @date: 2019年12月23日 下午 07:26:12
	 * @description: 菜单新增
	 * @param: dict
	 * @return:
	 */
	@Transactional(readOnly=false)
	public void add(Dict dict) {
		dict.preInsert();
		baseMapper.insert(dict);
	}

	@Transactional(readOnly=false)
	public void deleteDictById(String id) {
		dictMapper.deleteDictById(id);
	}
}
